package com.cxx.home.service.impl;

import com.cxx.home.dao.PeriodDao;
import com.cxx.home.dto.period.PeriodDayDto;
import com.cxx.home.dto.period.PeriodSettingDto;
import com.cxx.home.entity.PeriodDay;
import com.cxx.home.entity.PeriodSetting;
import com.cxx.home.mapper.PeriodDayMapper;
import com.cxx.home.mapper.PeriodSettingMapper;
import com.cxx.home.service.PeriodService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PeriodServiceImpl implements PeriodService {
    @Resource
    private PeriodDao periodDao;

    @Resource
    private PeriodDayMapper periodDayMapper;

    @Resource
    private PeriodSettingMapper periodSettingMapper;

    @Transactional
    @Override
    public Boolean updatePeriodDay(Long id, PeriodDayDto periodDayDto) {
        if (id == 0) {
            // 非月经天
            if (!periodDayDto.getIsMenstruate() && periodDayDto.getSexActivity().isEmpty()
                    && !periodDayDto.getIsDrug()) {
                return Boolean.TRUE;
            } else {
                PeriodDay periodDay = new PeriodDay();
                BeanUtils.copyProperties(periodDayDto, periodDay);
                periodDay.setIsDrug(periodDayDto.getIsDrug() ? 1 : 0);
                periodDay.setIsMenstruate(periodDayDto.getIsMenstruate() ? 1 : 0);
                return periodDayMapper.insert(periodDay) == 1;
            }
        } else {
            PeriodDay periodDay = new PeriodDay();

            // 非月经天
            if (!periodDayDto.getIsMenstruate() && periodDayDto.getSexActivity().isEmpty()
                    && !periodDayDto.getIsDrug()) {
                return periodDayMapper.deleteById(id) == 1;
            } else {
                BeanUtils.copyProperties(periodDayDto, periodDay);
                periodDay.setId(id);
                periodDay.setIsDrug(periodDayDto.getIsDrug() ? 1 : 0);
                periodDay.setIsMenstruate(periodDayDto.getIsMenstruate() ? 1 : 0);
                return periodDayMapper.updateById(periodDay) == 1;
            }
        }
    }

    @Override
    public List<PeriodDayDto> queryPeriodDay(String month) {
        return periodDao.queryPeriodDay(month);
    }

    @Override
    public String getMenstruateFirstDate(String month) {
        return periodDao.getMenstruateFirstDate(month);
    }

    @Override
    public Boolean updatePeriodSetting(Long id, PeriodSettingDto periodSettingDto) {
        if (id == 0) {
            PeriodSetting periodSetting = new PeriodSetting();
            BeanUtils.copyProperties(periodSettingDto, periodSetting);
            periodSetting.setIsCalculate(periodSettingDto.getIsCalculate() ? 1 : 0);
            return periodSettingMapper.insert(periodSetting) == 1;
        } else {
            PeriodSetting periodSetting = new PeriodSetting();
            BeanUtils.copyProperties(periodSettingDto, periodSetting);
            periodSetting.setId(id);
            periodSetting.setIsCalculate(periodSettingDto.getIsCalculate() ? 1 : 0);
            return periodSettingMapper.updateById(periodSetting) == 1;
        }
    }

    @Override
    public PeriodSettingDto queryPeriodSetting() {
        List<PeriodSetting> periodSettingList = periodSettingMapper.selectList(null);
        PeriodSettingDto periodSettingDto = new PeriodSettingDto();

        if (periodSettingList.size() > 0) {
            BeanUtils.copyProperties(periodSettingList.get(0), periodSettingDto);
            periodSettingDto.setIsCalculate(periodSettingList.get(0).getIsCalculate() == 1);
        } else {
            periodSettingDto.setId(0L);
            periodSettingDto.setCycleLength(0);
            periodSettingDto.setDuration(0);
            periodSettingDto.setIsCalculate(false);
        }

        return periodSettingDto;
    }
}
